package com.unicom.lottery.services;

import com.unicom.lottery.dao.LotteryMapper;
import com.unicom.lottery.entity.ReturnMsg;
import com.unicom.lottery.entity.input.*;
import com.unicom.lottery.entity.out.LotteryNameRecord;
import com.unicom.lottery.entity.out.UserLotteryRecord;
import com.unicom.lottery.util.LotteryManger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class LotteryServices {

    @Autowired
    LotteryMapper lotteryMapper;

    public ReturnMsg initLottery(){
        List<LotteryName> lotteryNames = lotteryMapper.initLottery();
        return  new LotteryNameRecord("SUC","",lotteryNames);
    }




    public ReturnMsg userGiftRecords(User user) {
        Map<String,Object> map=new HashMap<>();
        map.put("userId",user.getUserId());

        List<LotteryRecord> lotteryNames = lotteryMapper.userGiftRecords(map);
        return  new UserLotteryRecord("SUC","",lotteryNames);
    }

    @Transactional
    public ReturnMsg lottery(User user) throws  RuntimeException{
            //查询配置抽奖次数
            Map<String, Object> mapConfig = lotteryMapper.queryLotteryConfig("lottery_count");
            int configCount= (int) mapConfig.get("config_info");

            //查询用户抽奖次数
            Map<String,Object> mapUser=new HashMap<>();
            mapUser.put("userId",user.getUserId());
            int lotteryCount = lotteryMapper.userGiftRecords(mapUser).size();
            if(lotteryCount>=configCount){
                throw  new RuntimeException("超过配置抽奖次数！");
            }

            //开始抽礼品
            List<LotteryNameAll> list = lotteryMapper.initLotteryAll();//得到每个商品概率条件
            List<BigDecimal> rateList=new ArrayList<BigDecimal>();
            for(LotteryNameAll gift:list){
                BigDecimal bg = new BigDecimal(gift.getGiftprobability());
                rateList.add(bg);
            }

            //得到那个中奖
            int giftIndex=LotteryManger.getRandomIndex(rateList);
            LotteryNameAll gift=list.get(giftIndex);
            System.out.println("获得抽奖礼品Id-------------->"+gift.getId());

            //写入中奖信息
            Map<String,Object> map=new HashMap<>();
            map.put("userId",user.getUserId());
            map.put("giftId",gift.getId());

            int result = lotteryMapper.addLotteryRecord(map);
            lotteryMapper.reduceStock(gift.getId());

            //插入中奖信息失败
            if(result<1){
                throw  new RuntimeException("加入中奖记录错误！");
            }

            LotteryNameAll lotteryNameAll = lotteryMapper.initLotteryAllByUid(gift.getId());

            //库存已经用完
            if (lotteryNameAll.getGiftstock()<0){
                throw new RuntimeException("库存已经用完！");
            }

            return new ReturnMsg("SUC",gift.getId().toString());

    }

    /**
     * 查询用户可以抽奖次数
     * @return
     */
    public ReturnMsg initUserLotteryInfo(User user) {
        //查询配置抽奖次数
        Map<String, Object> mapConfig = lotteryMapper.queryLotteryConfig("lottery_count");
        int configCount= (int) mapConfig.get("lottery_count");

        //查询用户抽奖次数
        Map<String,Object> mapUser=new HashMap<>();
        mapUser.put("userId",user.getUserId());
        int lotteryCount = lotteryMapper.userGiftRecords(mapUser).size();

        int surplusCount=configCount-lotteryCount;

        if(configCount>=lotteryCount){
            return new ReturnMsg("SUC","0");
        }else{
            return new ReturnMsg("SUC",String.valueOf(surplusCount));
        }

    }

    /**
     * 添加用户分享信息
     * @param user
     * @return
     */
    @Transactional
    public ReturnMsg addShareInfo(LotteryUser user) {
        int result = lotteryMapper.addShareInfo(user);
        if(result>0)
            return  new ReturnMsg("SUC","");
        else
            return  new ReturnMsg("ERR","添加用户分享记录失败！");

    }

    /**
     * 获取活动描述信息
     * @param user
     * @return
     */
    public String activityDesc(String activityDesc) {
        Map<String, Object> mapConfig = lotteryMapper.queryLotteryConfig(activityDesc);
        return mapConfig==null ? "没有活动描述配置信息":mapConfig.get("config_info").toString();
    }

    /**
     * 填写用户中奖地址信息
     * @param fillAddressInfo
     * @return
     */
    public ReturnMsg fillLotteryRecord(FillAddressInfo fillAddressInfo) {
        int result = lotteryMapper.fillLotteryRecord(fillAddressInfo);
        if(result>0)
            return  new ReturnMsg("SUC","");
        else
            return  new ReturnMsg("ERR","添加地址信息失败");

    }
}
